package dmcs.inventorysystem.view.utils;


import java.util.Map;

import javax.faces.context.FacesContext;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.share.logging.ADFLogger;
import oracle.adf.view.rich.component.rich.data.RichTable;

import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;

import oracle.jbo.Key;
import oracle.jbo.uicli.binding.JUCtrlHierBinding;
import oracle.jbo.uicli.binding.JUCtrlHierNodeBinding;

import org.apache.myfaces.trinidad.event.SelectionEvent;
import org.apache.myfaces.trinidad.model.CollectionModel;


public class ADFUtils {

    private static final ADFLogger LOG = ADFLogger.createADFLogger(ADFUtils.class);

    public ADFUtils() {
        super();
    }

    public static void editEntry(RichTable table, String operationName, String navigationName) {
        LOG.entering("ADFUtils", "editEntry");
        LOG.fine("Entry params: " + table.getId() + " " + operationName + " " + navigationName);
        JUCtrlHierNodeBinding nodeBinding = (JUCtrlHierNodeBinding)table.getSelectedRowData();

        BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry();
        OperationBinding setCurrent = bindings.getOperationBinding(operationName);
        Map map = setCurrent.getParamsMap();
        map.put("rowKey", nodeBinding.getRow().getKey().toStringFormat(true));
        setCurrent.execute();
        if (!setCurrent.getErrors().isEmpty()) {
            LOG.severe("Set current entry errors: " + setCurrent.getErrors());
        }

        FacesContext fcx = FacesContext.getCurrentInstance();
        fcx.getApplication().getNavigationHandler().handleNavigation(fcx, null, navigationName);
        LOG.exiting("ADFUtils", "editEntry");
    }

    /**
     * Synchronizes the table UI component row selection with the
     * selection in the ADF binding layer
     * @param selectionEvent event object created by the table
     * component upon row selection
     */
    public static void makeCurrent(SelectionEvent selectionEvent) {
        LOG.entering("ADFUtils", "makeCurrent");
        RichTable _table = (RichTable)selectionEvent.getSource();
        //The Collection Model is the object that provides the structured data for the table to render.
        CollectionModel _tableModel = (CollectionModel)_table.getValue();
        //The ADF object that implements the CollectionModel is JUCtrlHierBinding.
        //It is wrapped by the CollectionModel API.
        JUCtrlHierBinding _adfTableBinding = (JUCtrlHierBinding)_tableModel.getWrappedData();
        //Acess the ADF iterator binding that is used with ADF table binding.
        DCIteratorBinding _tableIteratorBinding = _adfTableBinding.getDCIteratorBinding();

        //Cast to JUCtrlHierNodeBinding, which is the ADF object that represents a row
        JUCtrlHierNodeBinding _selectedRowData = (JUCtrlHierNodeBinding)_table.getSelectedRowData();
        
        //get the row key from the node binding and set it as the current row in the iterator
        Key _rwKey = _selectedRowData.getRowKey();
        _tableIteratorBinding.setCurrentRowWithKey(_rwKey.toStringFormat(true));
        LOG.exiting("ADFUtils", "makeCurrent");
    }
}
